/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.isqlviewer.util.LocalMessages;

/**
 * @author Markus A. Kobold &lt;mkobold at sprintpcs dot com&gt;
 */
public class LoginDialog extends JDialog implements ActionListener {

    private static final long serialVersionUID = -1870688453773372222L;

    private static final GridBagConstraints UI_CONSTRAINT = new GridBagConstraints(0, 0, 0, 0, 0, 0, 0, 0, new Insets(
            1, 1, 1, 1), 0, 0);

    private static final String BUNDLE_IDENTIFIER = "org.isqlviewer.swing.ResourceBundle";
    private static final LocalMessages messages = new LocalMessages(BUNDLE_IDENTIFIER);

    private JTextField principal = new JTextField("", 16);
    private JPasswordField credentials = new JPasswordField("", 16);
    private JButton okButton = new JButton(messages.getMessage("logindialog.okbutton"));
    private JButton cancelButton = new JButton(messages.getMessage("logindialog.cancelbutton"));
    private String[] tokens = null;

    public LoginDialog(Frame frameParent) {

        super(frameParent, messages.getMessage("logindialog.title"), true);
        initializeUI();
    }

    public String[] getAuthTokens() {

        return tokens;
    }

    public void actionPerformed(ActionEvent e) {

        Object src = e.getSource();
        if (src.equals(credentials) || src.equals(okButton)) {
            tokens = new String[2];
            tokens[0] = principal.getText();
            tokens[1] = new String(credentials.getPassword());
            dispose();
        } else if (src.equals(cancelButton)) {
            tokens = null;
            dispose();
        } else if (src.equals(principal)) {
            credentials.requestFocus();
        }
    }

    @Override
    protected void processWindowEvent(WindowEvent e) {

        switch (e.getID()) {
            case WindowEvent.WINDOW_OPENED :
                principal.requestFocus();
                principal.setText(System.getProperty("user.name"));
                setLocationRelativeTo(getOwner());
                break;
        }
        super.processWindowEvent(e);
    }

    private void initializeUI() {

        Component component = null;
        Object constraint = null;

        JPanel contentPanel = new JPanel(new GridBagLayout());

        component = new JLabel(messages.getMessage("logindialog.username.title"));
        ((JLabel) component).setLabelFor(principal);
        constraint = constrain(0, 0, 1, 1, 0.0d, 0.0d, GridBagConstraints.WEST, GridBagConstraints.NONE);
        contentPanel.add(component, constraint);

        component = new JLabel(messages.getMessage("logindialog.password.title"));
        ((JLabel) component).setLabelFor(credentials);
        constraint = constrain(0, 1, 1, 1, 0.0d, 0.0d, GridBagConstraints.WEST, GridBagConstraints.NONE);
        contentPanel.add(component, constraint);

        component = principal;
        constraint = constrain(1, 0, 1, 1, 1.0d, 1.0d, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
        contentPanel.add(component, constraint);

        component = credentials;
        constraint = constrain(1, 1, 1, 1, 1.0d, 1.0d, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
        contentPanel.add(component, constraint);

        component = Box.createVerticalStrut(10);
        constraint = constrain(0, 2, 2, 1, 0.0d, 0.0d, GridBagConstraints.WEST, GridBagConstraints.NONE);
        contentPanel.add(component, constraint);

        JComponent commandPanel = new JPanel(new GridBagLayout());
        JButton actionButton = null;

        constraint = constrain(0, 0, 1, 1, 1.0d, 0.0d, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
        commandPanel.add(Box.createHorizontalStrut(10), constraint);

        constraint = constrain(1, 0, 1, 1, 1.0d, 0.0d, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
        commandPanel.add(Box.createHorizontalGlue(), constraint);

        actionButton = okButton;
        actionButton.addActionListener(this);
        actionButton.setActionCommand(Integer.toString(0));
        actionButton.setToolTipText(messages.getMessage("logindialog.okbutton.tip"));
        constraint = constrain(2, 0, 1, 1, 0.0d, 0.0d, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        commandPanel.add(actionButton, constraint);

        actionButton = cancelButton;
        actionButton.addActionListener(this);
        actionButton.setActionCommand(Integer.toString(1));
        actionButton.setToolTipText(messages.getMessage("logindialog.cancelbutton.tip"));
        constraint = constrain(3, 0, 1, 1, 0.0d, 0.0d, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        commandPanel.add(actionButton, constraint);

        constraint = constrain(4, 0, 1, 1, 0.0d, 0.0d, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
        commandPanel.add(Box.createHorizontalStrut(10), constraint);

        constraint = constrain(0, 1, 5, 1, 0.0d, 0.0d, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
        commandPanel.add(Box.createVerticalStrut(10), constraint);

        getRootPane().setDefaultButton(okButton);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        Container container = new JPanel(new BorderLayout());
        container.add(contentPanel, BorderLayout.CENTER);
        container.add(commandPanel, BorderLayout.SOUTH);
        container.add(Box.createVerticalStrut(10), BorderLayout.NORTH);
        container.add(Box.createHorizontalStrut(10), BorderLayout.WEST);
        container.add(Box.createHorizontalStrut(10), BorderLayout.EAST);

        setContentPane(container);
        setResizable(false);
        pack();
    }

    private static GridBagConstraints constrain(int x, int y, int w, int h, double wx, double wy, int a, int f) {

        UI_CONSTRAINT.gridx = x;
        UI_CONSTRAINT.gridy = y;
        UI_CONSTRAINT.gridwidth = w;
        UI_CONSTRAINT.gridheight = h;
        UI_CONSTRAINT.weightx = wx;
        UI_CONSTRAINT.weighty = wy;
        UI_CONSTRAINT.anchor = a;
        UI_CONSTRAINT.fill = f;
        return UI_CONSTRAINT;
    }
}